import dayjs from "../../../libs/dayjs";
import { AstContext } from "../../context";
import { getCurrentPackageName } from "../../utils/getCurrentPackageName";
import { swipeUp } from "../../utils/pullDownToRefresh";
import { useRoot } from "../../utils/useRoot";
import { useStorage } from "../../utils/useStorage";
import { Pages } from "./Pages";
import { R } from "./R";
import { PageVisitor } from "./pageVisitor";
import { currentPage } from "./utils/consts";

export class RouterVisitor {
    visitOther(page: Pages, ctx: AstContext): void {
        ctx.call('close')
    }
    visitSplash(page: Pages, ctx: AstContext): void {
        ctx.call('close')
    }
    visitChildLock(page: Pages, ctx: AstContext): void {
        ctx.call('close')
    }
    visitHomeLiveLive(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeLiveHot:
                ctx.call('toSecondTabTitle', '热门')
                break;
            case Pages.homeLiveFriend:
                ctx.call('toSecondTabTitle', '朋友')
                break;
            case Pages.homeLiveLife:
                ctx.call('toSecondTabTitle', '生活')
                break;
            case Pages.homeLiveMove:
                ctx.call('toSecondTabTitle', '放映厅')
                break;
            case Pages.live:
            case Pages.homeLive:
            case Pages.homeLiveLive:
                break;
            case Pages.mine:
                ctx.call('toBottomTab', 3)
                break;
            case Pages.jingxuan:
                ctx.call('toBottomTab', 1)
                break;
            case Pages.message:
                ctx.call('toBottomTab', 2)
                break;
            case Pages.homeFollow:
                ctx.call('toTopTab', 1)
                break;
            case Pages.homeCity:
                ctx.call('toTopTab', 3)
                break;
            case Pages.homeFind:
                ctx.call('toTopTab', 2)
                break;
            case Pages.homeShop:
                ctx.call('toTopTab', 0)
                break;
            default:
                ctx.call('toSlideMenu')
                break;
        }
    }
    visitHomeShop(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeLive:
            case Pages.homeLiveFriend:
            case Pages.homeLiveHot:
            case Pages.homeLiveLife:
            case Pages.homeLiveLive:
            case Pages.homeLiveMove:
                if (ctx.call('toTopTab', 4)) {
                    ctx.set(currentPage, page)
                }
                break;
            default:
                ctx.call('toSlideMenu')
                break;
        }
    }
    visitHomeFollowSelect(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeLiveLive:
                ctx.call('toTopTab', 4)
                break;
            default:
                const followTabIndex = ctx.get<number>(`followTabIndex`)
                switch (followTabIndex) {
                    case 0:
                        ctx.call(`toFollowTab`, 1)
                        break;
                    case 1:
                    default:
                        ctx.call(`toFollowTab`, 0)
                }
                ctx.set('currentPage', Pages.homeFollow)
        }
    }
    visitUnknown(page: Pages, ctx: AstContext) {
        if (ctx.call('exit')) {
            sleep(1000)
            return;
        }
        if (ctx.call('close')) {
            sleep(1000)
            return;
        }
        if (getCurrentPackageName() !== 'com.smile.gifmaker') {
            app.launchPackage("com.smile.gifmaker")
            sleep(1000)
            return;
        } else {
            ctx.call('toBottomTab', 3)
            sleep(1000)
            const page = this.currentPage()
            if (page) return
        }
        toastLog(`visitUnknown error`)
        back()
        sleep(300)
        return;
    }
    private toHomeTab(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeLiveFriend:
                break;
            case Pages.homeLiveLife:
                break;
            case Pages.homeLiveMove:
                break;
            case Pages.live:
                break;
            case Pages.redPacket:
                break;
            case Pages.homeLiveLive:
                if (ctx.get(`currentTabIndex`) !== 4) {
                    ctx.call('toTopTab', 4)
                    ctx.set('currentPage', page)
                } else {
                    if (ctx.get('currentTab') === `直播`) {
                        ctx.set('currentPage', Pages.homeLiveLive)
                    }
                }
                break;
            case Pages.homeLiveHot:
                break
            case Pages.homeLive:
                if (ctx.get(`currentTabIndex`) !== 4) {
                    ctx.call('toTopTab', 4)
                }
                break;
            case Pages.homeFind:
                ctx.call('toTopTab', 2)
                break;
            case Pages.homeCity:
                ctx.call('toTopTab', 3)
                break;
            case Pages.homeFollow:
                ctx.call('toTopTab', 1)
                break;
            case Pages.homeShop:
                ctx.call('toTopTab', 0)
                break;
            default:
                ctx.call('toSlideMenu')
                break;
        }
    }
    visitHome(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.mine:
                ctx.call(`toBottomTab`, 3)
                break;
            case Pages.message:
                ctx.call(`toBottomTab`, 2)
                break;
            case Pages.jingxuan:
                ctx.call(`toBottomTab`, 1)
                break;
            default:
                this.toHomeTab(page, ctx)
        }
    }
    visitHomeLiveHot(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeLiveLive:
                ctx.call('toSecondTabTitle', '直播')
                break;
            default:
                break;
        }
    }
    visitJingXuan(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.mine:
                ctx.call('toBottomTab', 3)
                break;
            case Pages.message:
                ctx.call('toBottomTab', 2)
                break;
            case Pages.home:
            default:
                ctx.call('toBottomTab', 0)
        }
    }
    visitMessage(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.mine:
                ctx.call('toBottomTab', 3)
                break;
            case Pages.jingxuan:
                ctx.call('toBottomTab', 1)
                break;
            case Pages.home:
            default:
                ctx.call('toBottomTab', 0)
        }
    }
    visitMenu(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.storageSpace:
                const settings = ctx.get<any[]>(`settings`)
                if (settings && settings.length > 0) {
                    const one = settings.find(it => it.title === '设置')
                    if (one) {
                        one.click()
                        sleep(1000)
                    }
                }
                break;
            default:
                back();
                sleep(1000)
        }
    }
    visitMine(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.message:
                ctx.call('toBottomTab', 2)
                break;
            case Pages.jingxuan:
                ctx.call('toBottomTab', 1)
                break;
            case Pages.home:
                ctx.call('toBottomTab', 0)
                break;
            case Pages.mine:
                break;
            default:
                ctx.call('toBottomTab', 0)
        }
    }
    visitKwaiRn(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeFollow:
            case Pages.homeLive:
                back();
                break;
        }
        if (ctx.call('exit')) {
            sleep(1000)
            return;
        }
        if (ctx.call('close')) {
            sleep(1000)
            return;
        }
        back()
        sleep(1000)
    }
    visitLive(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.homeFollow:
            case Pages.homeLiveLive:
            case Pages.homeLive:
            case Pages.homeLiveFriend:
            case Pages.homeLiveHot:
            case Pages.homeLiveLife:
            case Pages.homeLiveMove:
            case Pages.home:
            case Pages.homeCity:
            case Pages.homeFind:
            case Pages.homeFollowSelect:
            case Pages.homeShop:
                back();
                sleep(1000)
                break;
            case Pages.share:
                if (ctx.call('toShare')) {
                    sleep(1000)
                } else {
                    const node = selector().visibleToUser().idEndsWith(R.live_bottom_bar_single_icon_container).findOnce()
                    if (node) {
                        node.click()
                        node.recycle()
                    } else {
                        toastLog(`没有找到分享按钮`)
                    }
                }
                break;
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    visitClose(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.homeFollow:
            case Pages.homeLiveLive:
                back();
                sleep(1000)
                break;
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    visitRedPacket(page: Pages, ctx: AstContext): void {
        switch (page) {
            default:
                back();
                sleep(1000)
        }
    }
    visitWebView(page: Pages, ctx: AstContext): void {
        switch (page) {
            default:
                back()
                sleep(1000)
        }
    }
    visitLiveBottomDialog(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.homeFollow:
            case Pages.homeLiveLive:
            case Pages.live:
                if (ctx.call('exit')) {
                    sleep(1000)
                } else {
                    back();
                    sleep(1000)
                }
                break;
            case Pages.share:
                const items = ctx.get<any[]>('list')
                if (items && items.length > 0) {
                    const item = items.find(it => it.title === '分享')
                    if (item) {
                        item.click()
                        return;
                    }
                } else {
                    const node = selector().visibleToUser().idEndsWith(R.live_bottom_bar_item_title_text_view).text(`分享`).findOnce()
                    if (node) {
                        node.click()
                        node.recycle()
                        return;
                    }
                }
                break;
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    visitLiveSlide(page: Pages, ctx: AstContext): void {
        switch (page) {
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    visitSettings(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.storageSpace:
                const items = ctx.get<any[]>(`items`)
                if (items && items.length > 0) {
                    const one = items.find(it => it.title === '存储空间')
                    if (one) {
                        one.click()
                        sleep(1000)
                    } else {
                        const listNode = ctx.get<AutoJs.UiObject>('listNode')
                        if (listNode) {
                            swipeUp(listNode.bounds())
                        }
                    }
                }
                break;
            default:
                back()
                sleep(1000)
        }
    }
    visitStorageSpace(page: Pages, ctx: AstContext): void {
        switch (page) {
            case Pages.storageSpace:
                sleep(1000)
                break;
            default:
                back()
                sleep(1000)
        }
    }
    visitHomeCity(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeShop:
                ctx.call('toTopTab', 0)
                break;
            case Pages.homeFollow:
                ctx.call('toTopTab', 1)
                break;
            case Pages.homeFind:
                ctx.call('toTopTab', 2)
                break;
            case Pages.homeCity:
                break;
            case Pages.homeLiveFriend:
            case Pages.homeLiveHot:
            case Pages.homeLiveLife:
            case Pages.homeLiveLive:
            case Pages.homeLiveMove:
            case Pages.homeLive:
                ctx.call('toTopTab', 4)
                break;
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    visitHomeFollow(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeShop:
                ctx.call('toTopTab', 0)
                break;
            case Pages.homeFollow:
                break;
            case Pages.homeFind:
                ctx.call('toTopTab', 2)
                break;
            case Pages.homeCity:
                ctx.call('toTopTab', 3)
                break;
            case Pages.homeLiveFriend:
                break;
            case Pages.homeLiveHot:
                break;
            case Pages.homeLiveLife:
                break;
            case Pages.homeLiveLive:
                if (ctx.get(`currentTabIndex`) !== 4) {
                    ctx.call('toTopTab', 4)
                } else {
                    if (ctx.get('currentTab') === `直播`) {
                        ctx.set('currentPage', Pages.homeLiveLive)
                    }
                }
                break;
            case Pages.homeLiveMove:
                break;
            case Pages.homeLive:
                break;
            case Pages.mine:
                ctx.call('toBottomTab', 3)
                break;
            case Pages.message:
                ctx.call('toBottomTab', 2)
                break;
            case Pages.jingxuan:
                ctx.call('toBottomTab', 1)
                break;
            default:
                ctx.call('toSlide')
        }
    }
    visitShare(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.share:
                break;
            default:
                back()
                sleep(1000)
        }
    }
    visitRedPacketResult(page: Pages, ctx: AstContext) {
        switch (page) {
            default:
                back()
                sleep(1000)
        }
    }
    visitHomeFind(page: Pages, ctx: AstContext) {
        switch (page) {
            case Pages.homeFollow:
                ctx.call('toTopTab', 1)
                break;
            case Pages.homeShop:
                ctx.call('toTopTab', 0)
                break;
            case Pages.homeCity:
                ctx.call('toTopTab', 3)
                break;
            case Pages.homeLive:
            case Pages.homeLiveLive:
            case Pages.homeLiveFriend:
            case Pages.homeLiveHot:
            case Pages.homeLiveMove:
            case Pages.homeLiveLife:
                if (ctx.call('toTopTab', 4)) {
                    ctx.set(currentPage, page)
                }
                break;
            case Pages.homeFind:
                break;
            default:
                console.log(`from ${ctx.get('currentPage')} to ${page}`)
        }
    }
    currentPage() {
        let ctx: AstContext = new AstContext(dayjs)
        const root = useRoot()
        const visitor = new PageVisitor()
        if(!root) return ``
        visitor.visit(root, ctx)
        const currentPage = ctx.get<Pages>(`currentPage`)
        return currentPage;
    }
    getCurrentContext() {
        let ctx: AstContext = new AstContext(dayjs)
        const root = useRoot()
        const visitor = new PageVisitor()
        if(!root) return ctx;
        visitor.visit(root, ctx)
        return ctx;
    }

    private storage = useStorage()
    visit(page: Pages, max: number = 20) {
        const start = this.storage.get('start', true)
        while (max > 0 && start) {
            const root = useRoot()
            if(!root) break;
            const ctx = new AstContext(dayjs)
            const visitor = new PageVisitor()
            visitor.visit(root, ctx)
            const currentPage = this.currentPage()
            if (currentPage === page) {
                break;
            }
            if (!currentPage) {
                this.visitUnknown(page, ctx)
            } else {
                const method = (this as any)[currentPage];
                if (method) {
                    method.bind(this)(page, ctx)
                    const currentPage = this.currentPage()
                    if (currentPage === page) {
                        break;
                    }
                }
                console.log(`from ${currentPage} to ${page}`)
            }
            max -= 1;
            sleep(1000)
        }
        return this.getCurrentContext()
    }
}

export const router = new RouterVisitor()